package com.retirement.information.service.impl;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retirement.children.mapper.OldChildrenMapper;
import com.retirement.children.domain.OldChildren;
import com.retirement.information.dto.OldInformationDto;
import com.retirement.information.mapper.OldInformationMapper;
import com.retirement.information.service.OldInformationService;
import com.retirement.information.domain.OldInformation;
import com.retirement.records.domain.OldHealthRecords;
import com.retirement.records.mapper.OldHealthRecordsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *信息管理服务实现类
 * </p>
 *
 * @author lijy
 * @since 2023-12-20
 */
@Service
public class OldInformationServiceImpl extends ServiceImpl<OldInformationMapper, OldInformation> implements OldInformationService {


    private final OldHealthRecordsMapper oldHealthRecordsMapper;

    @Resource
    private OldChildrenMapper oldChildrenMapper;

    public OldInformationServiceImpl(OldHealthRecordsMapper oldHealthRecordsMapper) {
        this.oldHealthRecordsMapper = oldHealthRecordsMapper;
    }


    /**
     * 基础信息分页查询
     */
    @Override
    public List<OldInformation> getList(OldInformationDto dto) {
        return baseMapper.getList(dto);
    }

    /**
     * 新增基础信息
     */
    @Override
    public int addInformation(OldInformation dto) {
        Assert.isTrue(ObjectUtil.isNotNull(dto.getName()), "无姓名信息");
        Assert.isTrue(ObjectUtil.isNotNull(dto.getAge()), "无年龄信息");
        dto.setId(IdUtil.simpleUUID());
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());

        return baseMapper.insert(dto);
    }

    /**
     * 修改基础信息
     */
    @Override
    public int updateInformation(OldInformation dto) {
        Assert.isTrue(ObjectUtil.isNotNull(dto.getId()), "无id信息");
        OldInformation bean = baseMapper.selectById(dto.getId());
        Assert.isTrue(ObjectUtil.isNotNull(bean), "该信息不存在");
        BeanUtils.copyProperties(dto,bean);
        bean.setUpdateTime(LocalDateTime.now());

        return baseMapper.updateById(bean);
    }

    /**
     * 基础信息删除
     */
    @Override
    public int deleteById(String id) {
        OldInformation oldInformation = baseMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotNull(oldInformation), "该信息不存在");
        oldInformation.setDeleted(1);
        OldHealthRecords oldHealthRecords = oldHealthRecordsMapper.selectOne(
                new LambdaQueryWrapper<OldHealthRecords>()
                        .eq(OldHealthRecords::getInformationId,id)
                        .eq(OldHealthRecords::getDeleted,0));
        Assert.isTrue(ObjectUtil.isNull(oldHealthRecords), "该用户已绑定健康档案，不可删除");
        oldChildrenMapper.delete(new LambdaQueryWrapper<OldChildren>()
                .eq(OldChildren::getInformationId,id));

        return baseMapper.updateById(oldInformation);
    }

    @Override
    public int deleteUserByIds(String[] userIds) {
        return 0;
    }

    @Override
    public OldInformation getdetail(String id) {
        return baseMapper.selectById(id);
    }
}
